package fe;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

import utils.Utils;
import extractor.BaseRecord;


public abstract class FeatureExtractorBase {
	private FeatureManager featureManager;
	public FeatureExtractorBase(FeatureManager manager) {
		featureManager = manager;
	}
	private HashSet<String> valueScope = new HashSet<String>();
	protected String feature = null;
	private static final String schemaPrefix = "@attribute";
	private static final String schemaSeperator = " ";
	
	
	public String getSchema() {
		if (isNorminal())
			return Utils.getRecord(new String[]{schemaPrefix, getFeatureName(), getFeatureValueScope()}, schemaSeperator);
		else
			return Utils.getRecord(new String[]{schemaPrefix, getFeatureName(), "real"}, schemaSeperator);
	}
	
	public String getFeatureValueScope() {
		StringBuilder builder = new StringBuilder();
		builder.append("{");
		Iterator<String> iter = valueScope.iterator();
		while(iter.hasNext()) {
			builder.append(iter.next());
			if (iter.hasNext())
				builder.append(",");			
		}
		builder.append("}");
		return builder.toString();
		
	}
	protected FeatureManager getFeatureManager() {
		return featureManager;
	}
	
	public abstract String getFeatureName();
	public abstract boolean isNorminal();
	
	public void initFeature(){
		valueScope.clear();
	};
	
	public void beginRequest() {
		feature = null;
	}
	
	public void endRequest() {
		feature = null;
	}
	
	protected abstract String extractFeature(ArrayList<BaseRecord> records, int pos) throws Exception;
	
	public String getFeature(ArrayList<BaseRecord> records, int pos) throws Exception {
		feature = extractFeature(records, pos);
		return feature;
	}
	
	public void checkFeature(String feature) {
		if (isNorminal()) {
			if (!feature.trim().equals("?"))
				valueScope.add(feature);
		}
	}
	
}
